var app = getApp()
var baseUrl = app.globalData.baseUrl
Page({
  data: {
    clickId:"",
    classifys: null,
    goods: null,
  },

  onLoad(options) {
    //获取数据库分类信息
    this.getClassify()
    this.getGoods()
  },
  onShow(){
    this.getGoods
  },
  onPullDownRefresh(){
    wx.showLoading({
      title: '刷新中',
    })
    this.getGoods()
    setTimeout(()=>{
      wx.hideLoading({
        success: (res) => {},
      })
    },500)
  },

  getClassify(){
    wx.request({
      url: baseUrl+'classify/getClassify',
      method: 'GET',
      success: res => {
        this.setData({
          classifys: res.data,
          clickId: res.data[0]._id //将数据库的第一个分类id 设置位默认点击的分类
        })
      }
    })
  },
  async getGoods(){
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: baseUrl+'goods/list',
      method: 'GET',
      success: res => {
        console.log(res)
        this.setData({
          goods: res.data
        })
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },
  onClickClassify(e){
    let classifyid = e.currentTarget.dataset.id
    this.setData({
      clickId: classifyid
    })
  },
  toGoodsInfo(e){
    let goodsId = e.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '/pages/goods/goods?goodsid=' + goodsId,
    })
    //增加商品点击量
    wx.request({
      url: baseUrl+'goods/click/'+goodsId,
      method: 'GET',
      success: res => {

      }
    })
  },
  confirmKeyWord(e){
    let keyWord = e.detail.value
    if(!keyWord){
      wx.showToast({
        title: '输入不能为空！',
        icon: 'error'
      })
    }else{
      //跳转页面并查询数据库
      wx.navigateTo({
        url: '/pages/searchRes/searchRes?keyWord='+ keyWord,
      })
      this.setData({
        searchWord: ''
      })
    }
  },
  setKeyWord(e){
    let keyWord = e.detail.value
    this.setData({
      searchWord: keyWord
    })
  },
  onSearch(){
    let keyWord = this.data.searchWord
    if(!keyWord){
      wx.showToast({
        title: '输入不能为空！',
        icon: 'error'
      })
    }else{
      //跳转页面并查询数据库
      wx.navigateTo({
        url: '/pages/searchRes/searchRes?keyWord='+ keyWord,
      })
      this.setData({
        searchWord: ''
      })
    }
  },

})